package com.minew.beaconset.step;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import java.util.Date;


public class StepThread extends Thread implements SensorEventListener, StepListener {

    private SensorManager sensorManager;
    Sensor accel;
    private StepDetector stepDetector;
    private long numStpes = 0;
    private long lastStpes = 0;
    boolean isRegiter = false;
    boolean isActivity = false;
    private Context context;
    private Date today;


    public StepThread(Context context) {
        this.context = context;
        initStepDetector();
    }


    @Override
    public void run() {
        if (!isRegiter) {
            sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
            isRegiter = true;
        }
    }

    public void mystop() {
        if (isRegiter) {
            sensorManager.unregisterListener(this);
            isRegiter = false;
        }


    }


    public void initStepDetector() {

        stepDetector = new StepDetector(this);
        sensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);
        accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        step(lastStpes);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            stepDetector.updateModel(isActivity);

            stepDetector.updateStep(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    public void step(long num) {

        numStpes += num;
        if (numStpes - lastStpes > 10) {
            lastStpes = numStpes;
            save(today,numStpes);
        }

    }

    public void save(Date date, long num)
    {

    }



}


